from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# 初始化浏览器驱动
driver = webdriver.Chrome()

# 访问一个网址
driver.get("https://www.jd.com")

try:
    # 设置显式等待，每0.5秒检查一次，最大等待时间10秒
    element = WebDriverWait(driver, 10, poll_frequency=0.5).until(
        EC.presence_of_element_located((By.ID, "key"))
    )
    print("目标元素已加载")

    # 截图保存
    driver.save_screenshot("wait_page08.png")
    print("截图已保存")

except Exception as e:
    print(f"等待条件未满足：{e}")

finally:
    # 关闭浏览器
    driver.quit()